package com.leftcode.test;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;
import android.opengl.GLUtils;

public class LeftCodeRenderer implements Renderer {

	//private float angle = 0f;
//	 private float anglex = 0f;
//	 private float angley = 0f;
//	 private float anglez = 0f;
	 private Context context;
		float xrot = 0.0f;
		float yrot = 0.0f;
		private int texture;
		private ByteBuffer vertices;
		private ByteBuffer triangles1, triangles2, triangles3, triangles4, triangles5, triangles6;
		private ByteBuffer colors;
		
		//private ByteBuffer  triangle_strip1, triangle_strip2, triangle_strip3, triangle_strip4, triangle_strip5, triangle_strip6;
		private ByteBuffer tvertices1, tvertices2, tvertices3, tvertices4, tvertices5, tvertices6;
	 
//	    private float anglex = 0f;
//	    private float angley = 0f;
//	    private float anglez = 0f;

	public LeftCodeRenderer(LeftCodeMain context){
		this.context = context;
		createBuffers();
	}
 
		public void onSurfaceCreated(GL10 gl, EGLConfig config) {
			gl.glDisable(GL10.GL_DITHER); // 颜色抖动据说可能严重影响性能，禁用
			gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);// 设置清除颜色缓冲区时用的RGBA颜色值
			
			gl.glEnable(GL10.GL_DEPTH_TEST);
			gl.glDepthFunc(GL10.GL_LEQUAL);
			gl.glClearDepthf(1f);
	 
			gl.glEnable(GL10.GL_TEXTURE_2D);
			loadTexture(gl);

		}
		public void onSurfaceChanged(GL10 gl, int width, int height) {
			// 宽高比
			float aspect = (float) width / (float) (height == 0 ? 1 : height);
	 
			// 设置视口
			gl.glViewport(0, 0, width, height);
	 
			// 设置当前矩阵堆栈为投影矩阵，并将矩阵重置为单位矩阵
			gl.glMatrixMode(GL10.GL_PROJECTION);
			gl.glLoadIdentity();
	 
			GLU.gluPerspective(gl, 45.0f, aspect, 0.1f, 200.0f);
			GLU.gluLookAt(gl, 5f, 5f, 5f, 0f, 0f, 0f, 0, 1, 0);
		}
		public void onDrawFrame(GL10 gl) {
			// 清除颜色缓冲
			gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
	 
			// 设置当前矩阵堆栈为模型堆栈，并重置堆栈，
			// 即随后的矩阵操作将应用到要绘制的模型上
			gl.glMatrixMode(GL10.GL_MODELVIEW);
			gl.glLoadIdentity();
			
			//gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, new float[]{5, 5, 5, 1}, 0);
			
			gl.glRotatef(xrot, 0.0f, 1.0f, 0.0f);
			gl.glRotatef(yrot, 1.0f, 0.0f, 0.0f);
//			// 将旋转矩阵应用到当前矩阵堆栈上，即旋转模型
//			gl.glRotatef(anglez, 0, 0, 1);
//			gl.glRotatef(angley, 0, 1, 0);
//			gl.glRotatef(anglex, 1, 0, 0);
//			anglex += 0.1; // 递增角度值以便每次以不同角度绘制
//			angley += 0.2;
//			anglez += 0.3;
		
			// 启用顶点数组、法向量、颜色数组
			gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
			//gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
			//gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
			gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
			
			// 设置正面
			gl.glFrontFace(GL10.GL_CW);

			// 设置顶点数组指针为 ByteBuffer 对象 vertices
			// 第一个参数为每个顶点包含的数据长度（以第二个参数表示的数据类型为单位）
			gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertices);
			//gl.glColorPointer(4, GL10.GL_FLOAT, 0, colors);
			
			// 绑定纹理
			gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);

			
			
			// 绘制 triangles 表示的三角形
			gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, tvertices1);
			gl.glDrawElements(GL10.GL_TRIANGLES, triangles1.remaining(), GL10.GL_UNSIGNED_BYTE, triangles1);
			
			gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, tvertices2);
			gl.glDrawElements(GL10.GL_TRIANGLES, triangles2.remaining(), GL10.GL_UNSIGNED_BYTE, triangles2);
			
			gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, tvertices3);
			gl.glDrawElements(GL10.GL_TRIANGLES, triangles3.remaining(), GL10.GL_UNSIGNED_BYTE, triangles3);

//			gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, triangle_strip1.remaining(),	GL10.GL_UNSIGNED_BYTE, triangle_strip1);
//			gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, triangle_strip2.remaining(),	GL10.GL_UNSIGNED_BYTE, triangle_strip2);
//			gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, triangle_strip3.remaining(),	GL10.GL_UNSIGNED_BYTE, triangle_strip3);
//			gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, triangle_strip4.remaining(),	GL10.GL_UNSIGNED_BYTE, triangle_strip4);
//			gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, triangle_strip5.remaining(),	GL10.GL_UNSIGNED_BYTE, triangle_strip5);
//			gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, triangle_strip6.remaining(),	GL10.GL_UNSIGNED_BYTE, triangle_strip6);

			// 禁用顶点、法向量、颜色数组
			gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
			gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
			//gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
		}
	 
		private void createBuffers() {
			// 创建顶点缓冲，顶点数组使用 float 类型，每个 float 长4个字节
			vertices = ByteBuffer.allocateDirect(data_vertices.length * 4);
			// 设置字节顺序为本机顺序
			vertices.order(ByteOrder.nativeOrder());
			// 通过一个 FloatBuffer 适配器，将 float 数组写入 ByteBuffer 中
			vertices.asFloatBuffer().put(data_vertices);
			// 重置Buffer的当前位置
			vertices.position(0);
	 
			// 创建索引缓冲，索引使用 byte 类型，所以无需设置字节顺序，也无需写入适配。
			triangles1 = ByteBuffer.allocateDirect(data_triangles1.length * 2);
			triangles1.put(data_triangles1);
			triangles1.position(0);
			
			triangles2 = ByteBuffer.allocateDirect(data_triangles2.length * 2);
			triangles2.put(data_triangles2);
			triangles2.position(0);

			triangles3 = ByteBuffer.allocateDirect(data_triangles3.length * 2);
			triangles3.put(data_triangles3);
			triangles3.position(0);
			
			triangles4 = ByteBuffer.allocateDirect(data_triangles4.length * 2);
			triangles4.put(data_triangles4);
			triangles4.position(0);
			
			triangles5 = ByteBuffer.allocateDirect(data_triangles5.length * 2);
			triangles5.put(data_triangles5);
			triangles5.position(0);
			
			triangles6 = ByteBuffer.allocateDirect(data_triangles6.length * 2);
			triangles6.put(data_triangles6);
			triangles6.position(0);



			colors = ByteBuffer.allocateDirect(data_colors.length * 4);
			colors.order(ByteOrder.nativeOrder());
			colors.asFloatBuffer().put(data_colors);
			colors.position(0);
			
			// 创建索引缓冲，索引使用 byte 类型，所以无需设置字节顺序，也无需写入适配。
			
//				triangle_strip1 = ByteBuffer.allocateDirect(data_triangle_strip1.length * 2);
//				triangle_strip1.put(data_triangle_strip1);
//				triangle_strip1.position(0);
//				
//				triangle_strip2 = ByteBuffer.allocateDirect(data_triangle_strip2.length * 2);
//				triangle_strip2.put(data_triangle_strip2);
//				triangle_strip2.position(0);
//				
//				triangle_strip3 = ByteBuffer.allocateDirect(data_triangle_strip3.length * 2);
//				triangle_strip3.put(data_triangle_strip3);
//				triangle_strip3.position(0);
//				
//				triangle_strip4 = ByteBuffer.allocateDirect(data_triangle_strip4.length * 2);
//				triangle_strip4.put(data_triangle_strip4);
//				triangle_strip4.position(0);
//				
//				triangle_strip5 = ByteBuffer.allocateDirect(data_triangle_strip5.length * 2);
//				triangle_strip5.put(data_triangle_strip5);
//				triangle_strip5.position(0);
//				
//				triangle_strip6 = ByteBuffer.allocateDirect(data_triangle_strip6.length * 2);
//				triangle_strip6.put(data_triangle_strip6);
//				triangle_strip6.position(0);
				
				tvertices1 = ByteBuffer.allocateDirect(data_tvertices1.length * 4);
				tvertices1.order(ByteOrder.nativeOrder());
				tvertices1.asFloatBuffer().put(data_tvertices1);
				tvertices1.position(0);
				
				tvertices2 = ByteBuffer.allocateDirect(data_tvertices2.length * 4);
				tvertices2.order(ByteOrder.nativeOrder());
				tvertices2.asFloatBuffer().put(data_tvertices2);
				tvertices2.position(0);
				
				tvertices3 = ByteBuffer.allocateDirect(data_tvertices3.length * 4);
				tvertices3.order(ByteOrder.nativeOrder());
				tvertices3.asFloatBuffer().put(data_tvertices3);
				tvertices3.position(0);
				
				tvertices4 = ByteBuffer.allocateDirect(data_tvertices4.length * 4);
				tvertices4.order(ByteOrder.nativeOrder());
				tvertices4.asFloatBuffer().put(data_tvertices4);
				tvertices4.position(0);
				
				tvertices5 = ByteBuffer.allocateDirect(data_tvertices5.length * 4);
				tvertices5.order(ByteOrder.nativeOrder());
				tvertices5.asFloatBuffer().put(data_tvertices5);
				tvertices5.position(0);
				
				tvertices6 = ByteBuffer.allocateDirect(data_tvertices6.length * 4);
				tvertices6.order(ByteOrder.nativeOrder());
				tvertices6.asFloatBuffer().put(data_tvertices6);
				tvertices6.position(0);

		}
		
		private void loadTexture(GL10 gl) {
			InputStream bitmapStream = null;
			Bitmap bitmap = null;
			try {
				bitmapStream = context.getResources().openRawResource(R.drawable.zhuanqiang1);
				bitmap = BitmapFactory.decodeStream(bitmapStream);
	 
				int[] textures = new int[1];
				gl.glGenTextures(1, textures, 0);
				texture = textures[0];
	 
				gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);
	 
				gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
						GL10.GL_NEAREST);
				gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
						GL10.GL_LINEAR);
	 
				gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,
						GL10.GL_REPEAT);
				gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,
						GL10.GL_REPEAT);
	 
				GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
	 
			} finally {
				if (bitmap != null)
					bitmap.recycle();
	 
				if (bitmapStream != null)
					try {
						bitmapStream.close();
					} catch (IOException e) {
					}
			}
		}

	 
		private float[] data_vertices = { 
				1, 1, 1, 
				1, -1, 1, 
				-1, -1, 1, 
				-1, 1, 1,
				1, 1, -1, 
				1, -1, -1, 
				-1, -1, -1, 
				-1, 1, -1, 
				};
		
		private float[] data_colors = { 
				1, 1, 0, 1,
				1, 0, 1, 1,
				0, 1, 1, 1,
				1, 0, 0, 1,
				0, 0, 0, 1,
				0, 0, 1, 1,
				0, 1, 0, 1,
				1, 1, 1, 1,
				};
		
		private byte[] data_triangles1 = {0, 1, 2, 	0, 2, 3,  };
		private byte[] data_triangles2 = {0, 3, 7, 	0, 7, 4,  };
		private byte[] data_triangles3 = {0, 4, 5, 	0, 5, 1, };	
		private byte[] data_triangles4 = {6, 5, 4, 	6, 4, 7,  };	
		private byte[] data_triangles5 = {6, 7, 3, 	6, 3, 2,  };	
		private byte[] data_triangles6 = {6, 2, 1, 	6, 1, 5,  };	
				

//		private byte[] data_triangle_strip1 =new byte[]{0,1,3,2,};  
//		private byte[] data_triangle_strip2 =new byte[]{4,5,7,6};
//		private byte[] data_triangle_strip3 =new byte[]{4,0,7,3};
//		private byte[] data_triangle_strip4 =new byte[]{1,5,2,6};
//		private byte[] data_triangle_strip5 =new byte[]{3,2,7,6};
//		private byte[] data_triangle_strip6 =new byte[]{4,5,0,1};
		
		//	private float [] data_tvertices1 = new float[]{1,0,  1, 1, 0, 0, 0, 1,    0, 0, 0, 0, 0, 0, 0, 0, };
		private float [] data_tvertices1 = new float[]{1,0, 1, 1,  0,1,  0, 0,    0,0, 0,0,0,0, 0,0, };
		private float [] data_tvertices2 = new float[]{1,0, 0,0, 0,0,		1, 1, 0,0,  0, 0, 0, 0,  0,1,  };
		private float [] data_tvertices3 = new float[]{0,0, 0,1, 0,0, 0,0,		 1,0, 1, 1,  0,0,  0, 0, };
		private float [] data_tvertices4 = new float[]{		 1,0, 1, 1,  0,1,  0, 0, };
		private float [] data_tvertices5 = new float[]{		 1,0, 1, 1,  0,1,  0, 0, };
		private float [] data_tvertices6 = new float[]{		 1,0, 1, 1,  0,1,  0, 0, };
}
